//BusinessThread
#pragma once
#include "Register/FPR.h"

class MarketSnapshot;
class VectorRegister;
class DealingCache;
class Timeout;
class TimeoutNotifier;

struct BusinessCommand;
struct OrderNotification;

class InternalBus
{
public:
	InternalBus(MarketSnapshot& marketSnapshot, VectorRegister& vectorRegister, DealingCache& dealingCache, TimeoutNotifier& timeoutNotifier);
	~InternalBus();
	void Run();

public:
	void OnCommand(BusinessCommand const& command);
	void OnOrderNotification(OrderNotification const& notification);
	void OnExchangeNotification(int exchangeStatus);
	void OnMarket(Market const& market);
	void OnPending(PendingItem const& pendingItem);
	void OnTimeout(Timeout const& timeout);

private:
	void AddBusiness(Business* businessPtr);
	void AddMarketHandler(IMarketHandler* marketHandler);
	
	void PushDealing(Dealing const& dealing);
	void PushTrade(Trade const& trade);
	bool ConvertOrderStatus2DealingStatus(OrderNotification const& notification, Dealing& dealing);
	void LogDealing(OrderNotification const& notification, Dealing const& dealing);
	void LogOrderNotification(Dealing const& dealing, OrderNotification const& notification) const;


private:
	MarketSnapshot& m_marketSnapshot;
	VectorRegister& m_vectorRegister;
	DealingCache& m_dealingCache;
	TimeoutNotifier& m_timeoutNotifier;
	FundsService m_fundsService;

	list<IMarketHandler*> m_marketHandlers;

	typedef std::vector<Business*> BusinessSet;
	BusinessSet m_businessSet;
 	vector<BusinessSet> m_contractBusinessList;
};	